package com.yisu.expressway.more_contact;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.reflect.TypeToken;
import com.yisu.expressway.R;
import com.yisu.expressway.activity.BaseActivity;
import com.yisu.expressway.databinding.ActivityMoreContactBinding;
import com.yisu.expressway.more_contact.model.ContactModel;
import com.yisu.expressway.network.ApiClientHelper;
import com.yisu.expressway.network.ApiResult;
import com.yisu.expressway.network.ApiSetting;
import com.yisu.expressway.utils.ToastUtil;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;


public class MoreContactActivity extends BaseActivity{

    ContactAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ActivityMoreContactBinding binding = DataBindingUtil.setContentView(this,R.layout.activity_more_contact);
        initView(binding);
        getContactList();
    }

    private void initView(ActivityMoreContactBinding binding){
        setTitle(R.string.more_contact);
        mAdapter = new ContactAdapter(this);
        binding.rvList.setLayoutManager(new LinearLayoutManager(this));
        binding.rvList.setAdapter(mAdapter);
    }

    private void getContactList(){

        showLoading();

        ApiClientHelper.postApi(ApiSetting.getContactNumberList(), new TypeToken<ArrayList<ContactModel>>() {
        }, new JSONObject(new HashMap()), new Response.Listener<ApiResult<ArrayList<ContactModel>>>() {
            @Override
            public void onResponse(ApiResult<ArrayList<ContactModel>> result) {

                dismissLoading();

                if (!result.success) {
                    ToastUtil.displayLongToastMsg(MoreContactActivity.this, result.getMsg());
                    return;
                }

                mAdapter.updateData(result.getResult());

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                dismissLoading();
                ToastUtil.displayShortToastMsg(MoreContactActivity.this, volleyError.getMessage());
            }
        }, this);

    }

}
